unit u_controls;

interface

uses classes, buttons, menus, stdctrls, cc_dict, sysutils;

function getControlCaption(cmp: TComponent):string;
procedure setControlCaption(cmp: TComponent; caption: string);
procedure loadControlsCaption(dest: TComponent; pref,dir:string);
procedure saveControlsCaption(dest: TComponent; pref:string);


implementation



function getControlCaption(cmp: TComponent):string;
begin
  result := '';
  if cmp is TSpeedButton then result := (cmp as TSpeedButton).caption else
  if cmp is TMenuItem then result := (cmp as TMenuItem).caption else
  if cmp is TLabel then result := (cmp as TLabel).caption;
end;

procedure setControlCaption(cmp: TComponent; caption: string);
begin
  if cmp is TSpeedButton then (cmp as TSpeedButton).caption := caption else
  if cmp is TMenuItem then (cmp as TMenuItem).caption := caption else
  if cmp is TLabel then (cmp as TLabel).caption := caption;
end;

procedure loadControlsCaption(dest: TComponent; pref,dir:string);
var
 st: TStrings;
 cVal, cName, fileName: String;
 dict: CCDict;
 a: TComponent;
 i: integer;
begin
 fileName :=dir+'lang\'+pref+'\'+dest.Name+'_words.'+pref;
 if not fileexists(fileName) then exit;
 dict:= CCDict.create;
 dict.loadFromFile(fileName);
 for i:=0 to dest.componentcount-1 do begin
  a := dest.components[i];
  cName := a.Name;
  cVal := dict.getValue(a.owner.name+'.'+cName);
  if cVal <> '' then begin
   setControlCaption(a, cVal);
  end;
 end;
 dict.free;
end;


procedure saveControlsCaption(dest: TComponent; pref:string);
var
 i:integer;
 a:TComponent;
 res, caption, name: string;
 st: TStrings;

begin
 st := TStringList.create;
 for i:=0 to dest.ComponentCount-1 do begin
  a:=dest.Components[i];
  if a.HasParent then pref := a.Owner.Name;
  caption := trim(getControlCaption(a));
  if caption = '' then continue;
  name:=pref+'.'+a.Name+' = '+caption;
  st.add(name);
 end;
 st.SaveToFile('lang\ru\'+pref+'_words.ru');
 st.free;
end;

end.
